package com.journaldev.threadpool.poolexecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 监控线程在特定的时间间隔内打印 executor 信息
 * @author:chenhs E-mail:hson08_chen@163.com
 * Created on 2017年9月8日 上午11:46:08
 */
public class MyMonitorThread implements Runnable {

	private ThreadPoolExecutor executor;
	
	private int seconds;
	
	private boolean run = true;
	
	public MyMonitorThread(ThreadPoolExecutor executor, int delay) {
		this.executor = executor;
		this.seconds = delay;
	}

	@Override
	public void run() {
		
		while(run){
			System.out.println(
				String.format("[monitor] [%d/%d] Active: %d, Completed: %d, Task: %d, isShutdown: %s, isTerminated: %s"
						, this.executor.getPoolSize()
						, this.executor.getCorePoolSize()
						, this.executor.getActiveCount()
						, this.executor.getCompletedTaskCount()
						, this.executor.getTaskCount()
						, this.executor.isShutdown()
						, this.executor.isTerminated())
				);
			
			try {
				Thread.sleep(seconds*1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
	}

}
